//
//  TitleViewTwo.m
//  UI_礼物说
//
//  Created by mac on 16/3/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "TitleViewTwo.h"
@interface TitleViewTwo()

@property(nonatomic, retain)NSArray *buttonArray;
@property(nonatomic,retain)UIView *indicatorView;/**< 选中框 */
@property(nonatomic, retain)UIButton *selectButton;
@property(nonatomic, assign)BOOL isClick;

@end
@implementation TitleViewTwo
+(id)creatTitleViewTwo
{
    return [[self alloc]init];
}
-(instancetype)init
{
    if (self = [super init]) {

    }
    return self;
}
-(UIButton *)selectedButton
{
    return _buttonArray[_selected];
}
-(void)setTitleArray:(NSArray *)titleArray
{
    NSMutableArray *buttonArray = [NSMutableArray arrayWithCapacity:titleArray.count];
    _titleArray = titleArray;
    float x = 0;
    for (NSString *title in titleArray) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:title forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        button.backgroundColor = [UIColor whiteColor];
        
        button.frame = CGRectMake(x, 0, BOUNDS_WIDTH / 3, 50);
        
        // 正常时候字体颜色
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        // 选中后字体颜色
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
//        // 把按钮背景图片设置为颜色
//        [button setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:54 / 255.0 green:54 / 255.0 blue:54 / 255.0 alpha:1]] forState:UIControlStateSelected];
        
        button.layer.cornerRadius = 4;
        button.clipsToBounds = YES;
        [self addSubview:button];
        [buttonArray addObject:button];
        if (x == 0) {
            button.selected = YES;
        }
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        x += BOUNDS_WIDTH / 3;
    }
    self.buttonArray = buttonArray;
    self.contentSize = CGSizeMake(x, 1);
}

#pragma mark - 背景图片通过颜色来表示
//  颜色转换为背景图片
//- (UIImage *)imageWithColor:(UIColor *)color {
//    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
//    UIGraphicsBeginImageContext(rect.size);
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    
//    CGContextSetFillColorWithColor(context, [color CGColor]);
//    CGContextFillRect(context, rect);
//    
//    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    
//    return image;
//}


-(void)buttonClicked:(UIButton *)button
{
    NSUInteger newIndex = [_buttonArray indexOfObject:button];
    if (newIndex == _selected) {
        return;
    }
    _selected = newIndex;
    [self setSelectedIndex:newIndex];
}



-(void)setSelectedIndex:(NSUInteger)selectedIndex
{
    self.selected = selectedIndex;
    for (UIButton * button in self.buttonArray) {
        button.selected = NO;
    }
    self.selectedButton.selected = YES;
    [UIView animateWithDuration:0.2 animations:^{
        CGRect  rect = [self  convertRect: CGRectMake(self.selectedButton.frame.origin.x-self.frame.size.width/2, self.selectedButton.frame.origin.y, self.frame.size.width, self.selectedButton.frame.size.height) toView:self];
        [self scrollRectToVisible:rect  animated:YES];
    

    }];
     //-(BOOL) respondsToSelector: selector 用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递)
    if (self.delegates && [self.delegates respondsToSelector:@selector(TitleViewTwo:selectedIndexChanged:)]) {
        
        [self.delegates TitleViewTwo:self selectedIndexChanged:selectedIndex];
        
    }

}
//通知接收者他的父视图将会改变到特定的父视图
//参数 newSuperview 新的视图对象将会是接收者新的父视图
- (void)willMoveToSuperview:(UIView *)newSuperview{
    
    self.frame = CGRectMake(0, 64, newSuperview.frame.size.width, 55);
    
}




@end
